package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.CourseSubjectHubVO;
import com.gibs.peixunapi.VO.ExerciseVO;
import com.gibs.peixunapi.dao.CourseSubjectHubDao;
import com.gibs.peixunapi.dao.ExercisesDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.mapper.ExercisesMapper;
import com.gibs.peixunapi.model.CourseSubjectHub;
import com.gibs.peixunapi.model.Exercises;
import com.gibs.peixunapi.model.SubjectHub;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.ExercisesService;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ExercisesServiceImpl implements ExercisesService{

	@Autowired
	private ExercisesDao exercisesDao;
	@Autowired
	private CourseSubjectHubDao courseSubjectHubDao;
	@Autowired
	private ExercisesMapper exercisesMapper;

	@Transactional
@Override
	public Result create(Exercises object) {
		try {
		
			exercisesDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(Exercises object) {
		try {
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {

			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
			HashMap<String, Object> data = new HashMap<String, Object>();



			return ResultUtil.success(ResultEnum.FIND_SUCCESS, data);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
			List<Exercises> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = exercisesDao.findLike("%" + key + "%");
			} else {
				list = exercisesDao.findAll();
			}

			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
			List<Exercises> list = new ArrayList<>();
			limit = limit == null ? 10 : limit;
			current = current == null ? 1 : current;
			int offset = (current - 1) * limit;
			Long total = null;
			if (!StringUtils.isEmpty(key)) {
				list = exercisesDao.findPageLike("%" + key + "%", offset, limit);
				total = exercisesDao.countLike("%" + key + "%");
			} else {
				list = exercisesDao.findPage(offset, limit);
				total = exercisesDao.count();
			}

			return ResultUtil.successPage(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
			List<Exercises> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = exercisesDao.findLike("%" + key + "%");
			} else {
				list = exercisesDao.findAll();
			}
			List<Object> data = new ArrayList<>();
			for (Exercises object : list) {
				HashMap<String, Object> map = new HashMap();
				map.put("value", object.getId());
//				map.put("name", object.getVideoClass_id());
				data.add(map);
			}
			return ResultUtil.success(ResultEnum.SUCCESS, data);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	@Transactional
	@Modifying(clearAutomatically = true)
	public Result changeExercise(List<ExerciseVO> exerciseVOList) {
		List<Exercises> exercisesList = new ArrayList<>(exerciseVOList.size());
		for (ExerciseVO exerciseVO : exerciseVOList) {
			Exercises exercises = exercisesDao.findById(exerciseVO.getId()).orElseThrow(EntityNotFoundException::new);
			if (!exercises.getCourseSubjectHub().getId().equals(exerciseVO.getId())){
				CourseSubjectHub courseSubjectHub = courseSubjectHubDao.findById(exerciseVO.getId()).orElseThrow(EntityNotFoundException::new);
				exercises.setCourseSubjectHub(courseSubjectHub);
			}
			exercises.setSort(exerciseVO.getSort());
			exercisesList.add(exercises);
		}
		exercisesDao.saveAll(exercisesList);
		return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
	}


	@Override
	@Transactional
	@Modifying(clearAutomatically = true)
	public Result exerciseLink(Integer linkCurseId, Integer lickedCourseId) {

		return null;//TODO
	}

	@Override
	public List<Integer> getSubjectHubIdList(Integer chapterId, String type) {
		List<CourseSubjectHubVO> courseSubjectHubList = exercisesMapper.getCourseSubjectHubList(chapterId, type);
		if(courseSubjectHubList.isEmpty()){
			throw new EntityNotFoundException();
		}
		return courseSubjectHubList.stream().map(CourseSubjectHubVO::getId).collect(Collectors.toList());
	}
}

